package com.right.health.app.ui.registered;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.magic.basic.utils.CollectionUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.base.BasePresenterActivity;
import com.right.health.app.bean.PayClinicOrderBean;
import com.right.health.app.net.OrderApi;
import com.vendor.social.utils.ToastUtil;

import org.json.JSONObject;

/**
 * 诊间支付详情
 */
public class PayClinicDetailActivity extends BasePresenterActivity implements OnHttpListener {
    public static final String DETAIL_ID_KEY = "orderId";
    private static final int PAY_TYPE_WX = 10;
    private static final int PAY_TYPE_ALI_PAY = 11;
    private static final int PAY_TYPE_SOCIAL = 12;

    private String mId;
    private TextView mHosTv, mPatientTv, mPatientIdTv, mCostTv,mWayTv,mTimeTv;
    private LinearLayout mCostItemLL;
    private OrderApi mOrderApi;
    private PayClinicOrderBean mOrderBean;

    public static void launch(Context context, String orderId) {
        Bundle bundle = new Bundle();
        bundle.putString(DETAIL_ID_KEY, orderId);
        Intent intent = new Intent(context, PayClinicDetailActivity.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.activity_pay_clinic_detail);
    }

    @Override
    public void findView() {
        mHosTv = findViewById(R.id.tv_hospital);
        mPatientTv = findViewById(R.id.tv_patient);
        mPatientIdTv = findViewById(R.id.tv_patient_id);
        mCostTv = findViewById(R.id.tv_money);
        mCostItemLL = findViewById(R.id.ll_cost_item);
        mWayTv = findViewById(R.id.tv_way);
        mTimeTv = findViewById(R.id.tv_time);
    }

    @Override
    public void initialize() {
        if (getIntent().getExtras() != null) {
            mId = getIntent().getExtras().getString(DETAIL_ID_KEY);
        }
        if (TextUtils.isEmpty(mId)) {
            ToastUtil.show(this, "订单不存在");
            finish();
        } else {
            mOrderApi = new OrderApi();
            mOrderApi.setListener(this);
            mOrderApi.getPayClinicDetail(mId);
        }
    }

    private void refreshUI() {
        mHosTv.setText("攀大附院");
        mPatientTv.setText(mOrderBean.getMembername());
        if (!TextUtils.isEmpty(mOrderBean.getPayamout())) {
            mCostTv.setText(mOrderBean.getPayamout()+"元");
        }
        mPatientIdTv.setText(mOrderBean.getMemberid());
        mWayTv.setText(getPayWay(mOrderBean.getPaytype()));
        mTimeTv.setText(mOrderBean.getPaytime());
        if (!CollectionUtil.isEmpty(mOrderBean.getDetails())) {
            View line = new View(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getResources().getDimensionPixelSize(R.dimen.dp_0_5));
            params.leftMargin = getResources().getDimensionPixelOffset(R.dimen.dp_16);
            params.rightMargin = getResources().getDimensionPixelOffset(R.dimen.dp_16);
            line.setBackgroundColor(getResources().getColor(R.color.color_EDEDED));
            for (int i=0;i<mOrderBean.getDetails().size();i++){
                View layout = LayoutInflater.from(this).inflate(R.layout.layout_common_item, null);
                TextView drugTv = layout.findViewById(R.id.tv_left);
                TextView priceTv = layout.findViewById(R.id.tv_right);
                drugTv.setText(mOrderBean.getDetails().get(i).getItemname());
                if (!TextUtils.isEmpty(mOrderBean.getDetails().get(i).getItemprice())) {
                    priceTv.setText("￥"+mOrderBean.getDetails().get(i).getItemprice());
                }
                mCostItemLL.addView(layout);
                if (i < mOrderBean.getDetails().size() - 1) {
                    mCostItemLL.addView(line,params);
                }
            }
        }

    }

    private String getPayWay(int paytype) {
        if (paytype == PAY_TYPE_WX) {
            return "微信";
        }else if (paytype==PAY_TYPE_ALI_PAY){
            return "支付宝";
        } else if (paytype == PAY_TYPE_SOCIAL) {
            return "社保";
        }
        return "";
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (!response.isResponseFail()) {
            try {
                    JSONObject jsonObject = new JSONObject(response.data);
                    if (!TextUtils.isEmpty(jsonObject.optString("paiddetail"))) {
                        mOrderBean = new Gson().fromJson(jsonObject.optString("paiddetail"), PayClinicOrderBean.class);
                        refreshUI();
                    }


            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {
            Toast.makeText(this, "请求失败", Toast.LENGTH_SHORT);
        }
    }
}
